package com.syh.jn.scm.service.impl.upstream.base;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.upstream.entity.base.UpBankAccount;
import com.syh.jn.scm.domain.upstream.repository.base.UpBankAccountRepository;
import com.syh.jn.scm.dto.upstream.base.UpBankAccountMto;
import com.syh.jn.scm.dto.upstream.base.UpBankAccountSto;
import com.syh.jn.scm.dto.upstream.base.UpBankAccountVo;
import com.syh.jn.scm.service.api.upstream.base.UpBankAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 银行账户管理
 */
@Service
@Transactional
public class UpBankAccountServiceImpl extends AbstractServiceImpl<UpBankAccountSto, UpBankAccountMto, UpBankAccountVo, UpBankAccount, Long> implements UpBankAccountService {
  @Autowired
  private UpBankAccountRepository upBankAccountRepository;

  @Override
  protected AggregateRepository<UpBankAccount, Long> getAggregateRepository() {
    return upBankAccountRepository;
  }

  /**
   * 保存银行账户
   * @param sto
   * @return
   */
  @Override
  public Long save(UpBankAccountSto sto) {
    UpBankAccount upBankAccount = UpBankAccount.createBusiness(sto);
    return upBankAccountRepository.add(upBankAccount);
  }

  /**
   * 修改银行账户
   * @param mto
   */
  @Override
  public void update(UpBankAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);

    //根据id查找对应的银行账户并判空
    UpBankAccount upBankAccount = this.getUpBankAccountById(mto.getId());
    upBankAccount.modify(mto);
    upBankAccountRepository.update(upBankAccount);
  }

  /**
   * 删除银行账户
   * @param id
   */
  @Override
  public void delete(Long id) {
    //根据id查找对应的银行账户并判空
    UpBankAccount upBankAccount = this.getUpBankAccountById(id);

    upBankAccount.destroy();
    upBankAccountRepository.removeById(id);
  }

  /**
   * 启用
   */
  @Override
  public void enabled(Long id) {
    //根据id查找对应的银行账户并判空
    UpBankAccount upBankAccount = this.getUpBankAccountById(id);

    upBankAccount.enabled();
    upBankAccountRepository.update(upBankAccount);
  }

  /**
   * 停用
   */
  @Override
  public void disabled(Long id) {
    //根据id查找对应的银行账户并判空
    UpBankAccount upBankAccount = this.getUpBankAccountById(id);

    upBankAccount.disabled();
    upBankAccountRepository.update(upBankAccount);
  }

  /**
   * 根据id查找对应的银行账户并判空
   * @param id
   * @return
   */
  private UpBankAccount getUpBankAccountById(Long id){
    ExtAssert.notNull(id, "id为空");

    UpBankAccount upBankAccount = upBankAccountRepository.getById(id);
    ExtAssert.notNull(upBankAccount, "对象为空");

    return upBankAccount;
  }
}
